<?php
    // 插入1000条数据
    include_once 'DB.php';
    $pdo_obj = new DB();
    $totalArray = $pdo_obj->query('select count(*) from register');
    $totalRows = $totalArray[0]['count(*)'];
    $pageSize = 10;
    $totalPages = ceil($totalRows/$pageSize);
    echo $totalPages;
    
    
    $page = $_GET['page']?$_GET['page']:1;
    if ($page < 1 || $page == '' || !is_numeric($page)) {
        $page = 1;
    }
    
    if ($page > $totalPages) {
        $page = $totalPages;
    }
    $offset = ($page-1)*$pageSize;
    $row_count = $pdo_obj->query('create table if not exists register (id int primary key auto_increment, name varchar(200),psw varchar(200),age int,time date)');
    $all_user = $pdo_obj->query("select * from register limit {$offset},{$pageSize};");

    if(!$all_user) {
        echo '没有用户!请先注册';
        $totalPages = 0;
    }
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style>
            table {
                width:80%;
                background-color: lightblue;
                border-collapse: collapse;
                margin:0 auto;
            }
            td {
                border:1px solid white;
                text-align: center;
            }
        </style>
    </head>
    <body>
        <h1>用户列表</h1>
        请输入关键字
        <input type="text" value="请输入要搜索的关键字" id="search"></input>
        <table>
            <tr>
                <td>编号</td>
                <td>用户名</td>
                <td>年龄</td>
                <td>注册时间</td>
            </tr>
            <?php $i=0;?>
            <?php foreach ($all_user as $key => $value) { ?>
            <?php $i++;?>
                <tr>
                    <td><?php echo $value['id'];?></td>
                    <td><?php echo $value['name'];?></td>
                    <td><?php echo $value['psw'];?></td>
                    <td><?php echo $value['time'];?></td>
                </tr>
            <?php } ?>    
        </table>
        
        <p style="text-align: center;">
            <a href="index.php?page=1">首页</a>
            <a href="index.php?page=<?php echo $page-1;?>">上一页</a>
            <?php for($i=1;$i<=$totalPages;$i++) { ?>
            <a href="index.php?page=<?php echo $i;?>">[<?php echo $i;?>]</a>
            
            <?php } ?>
            <a href="index.php?page=<?php echo $page+1;?>">下一页</a>
            <a href="index.php?page=<?php echo $totalPages;?>">尾页</a>
        </p>
        <?php echo '<hr>';?>
        <p style="text-align: center;">
            
           <?php
                $pageStr = '';
                if ($page == 1) {
//                    $pageStr .= "[首页][上一页]";
                } else {
                    $pageStr .= "<a href='index.php?page=1'>首页</a>";
                    $pageStr .= "<a href='index.php?page=".($page-1)."'>上一页</a>";
                }
                    
                if($totalPages > $pageSize) {
                    for($i=1;$i<=$totalPages;$i++) {
                        if ($page == $i) {
                            $pageStr.= "[$i]";
                        } else {
                            $pageStr.= "<a href='index.php?page={$i}'>[{$i}]</a>";
                        }
                    }
                } else {
                    $pageStr.= '共一页';
                }
                
                if ($page == $totalPages) {
//                    $pageStr .= "[下一页][尾页]";
                } else {
                    $pageStr .= "<a href='index.php?page=".($page+1)."'>下一页</a>";
                    $pageStr .= "<a href='index.php?page=".($totalPages)."'>尾页</a>";
                }
                $pageStr .= "<a>总页数{$totalPages}</a>";
                echo $pageStr;
           ?> 
            <?php echo '<hr>';?>
            <p style="text-align: center;">仿百度
            
           <?php
                $pageStr = '';
                if ($page == 1) {
//                    $pageStr .= "[首页][上一页]";
                } else {
                    $pageStr .= "<a href='index.php?page=1'>首页</a>";
                    $pageStr .= "<a href='index.php?page=".($page-1)."'>上一页</a>";
                }
                    
                $start = $page - 5;
                if ($start <= 1) {
                    $start = 1;
                }             
                $end = $page + 4;
                if ($end <= 10) {
                    $end = 10;
                }
                if ($end > $totalPages) {
                    $end = $totalPages;
                }
                for ($i=$start;$i<$end;$i++) {
                    if ($page == $i) {
                            $pageStr.= "[$i]";
                        } else {
                            $pageStr.= "<a href='index.php?page={$i}'>[{$i}]</a>";
                        }
                }
                if ($page == $totalPages) {
//                    $pageStr .= "[下一页][尾页]";
                } else {
                    $pageStr .= "<a href='index.php?page=".($page+1)."'>下一页</a>";
                    $pageStr .= "<a href='index.php?page=".($totalPages)."'>尾页</a>";
                }
                $pageStr .= "<a>总页数{$totalPages}</a>";
                echo $pageStr;
           ?>  
        </p>
        <?php echo '<hr>';?>
        <p style="text-align: center;">仿百度 
           <?php
                $pageStr = '';
                if ($page == 1) {
//                    $pageStr .= "[首页][上一页]";
                } else {
                    $pageStr .= "<a href='index.php?page=1'>首页</a>";
                    $pageStr .= "<a href='index.php?page=".($page-1)."'>上一页</a>";
                } 
                $start = $page - 5;
                if ($start <= 1) {
                    $start = 1;
                }
                $end = $page + 4;
                if ($end <= 10) {
                    $end = 10;
                }
                if ($end > $totalPages) {
                    $end = $totalPages;
                }
                for ($i=$start;$i<$end;$i++) {
                    if ($page == $i) {
                            $pageStr.= "[$i]";
                        } else {
                            $pageStr.= "<a href='index.php?page={$i}'>[{$i}]</a>";
                        }
                }
                if ($page == $totalPages) {
//                    $pageStr .= "[下一页][尾页]";
                } else {
                    $pageStr .= "<a href='index.php?page=".($page+1)."'>下一页</a>";
                    $pageStr .= "<a href='index.php?page=".($totalPages)."'>尾页</a>";
                }
                $pageStr .= "<a>总页数{$totalPages}</a>";
                echo $pageStr;
           ?>  
        </p>
        <form method="get" action="index.php">
            第<input type="text" style="width:40px;" name="page" id="showPage"/>页
            <input type="submit" value="跳转"/>
        </form>
        <script>
            var showPage = document.querySelector('#showPage');
            showPage.addEventListener('blur', function() {
                var val = parseInt(this.value, 10);
                location.href = "<?php echo 'index.php?page='?>"+val;
            });
            showPage.addEventListener('keydown',function(e){
                e = e||window.event;
                if (e.keyCode == 13) {
                    alert("回车了");
                    var val = parseInt(this.value, 10);
                    location.href = "<?php echo 'index.php?page='?>"+val;
                }
            });
                
        </script>
        
         <?php echo '<hr>';?>
        请选择跳转页面
        <select id="showPage1">
            <?php for ($i=1;$i<$totalPages;$i++) { ?>
            <option><?php echo $i;?></option>
            <?php  }?> 
        </select>
        
        <script>
            var showPage1 = document.querySelector('#showPage1');
            showPage1.addEventListener('change',function(){
                var val = parseInt(this.value, 10);
                alert(this.value);
                location.href = "<?php echo 'index.php?page='?>"+val;
            });
        </script>
    </body>
</html>
